package com.imagination.applet;


import com.imagination.applet.config.properties.SpringDocProperties;
import com.imagination.applet.controller.api.enums.TrainingStatusEnum;
import com.imagination.applet.domain.utils.SpringUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import com.imagination.applet.domain.product.service.ProductImageQueryService;
import com.imagination.applet.domain.product.model.ProductImage;
import com.imagination.applet.domain.revenue.identity.service.VirtualIdentityImageService;

import java.util.List;

@SpringBootApplication
@EnableScheduling
@Slf4j
@RequiredArgsConstructor
public class AppletWebApplication {
	private final ProductImageQueryService productImageQueryService;
	private final VirtualIdentityImageService virtualIdentityImageService;


	public static void main(String[] args) {
		ConfigurableApplicationContext context = SpringApplication.run(AppletWebApplication.class, args);
		SpringUtil.setAppContext(context);
		Environment bean = context.getBean(Environment.class);
		System.out.println(bean.getProperty("springdoc.api-docs.enabled"));
		System.out.println(bean);
	}

	@Scheduled(cron = "0/30 * * * * ?")
	public void checkRunningTaskStatus() {
		List<ProductImage> productImages = productImageQueryService.getByTrainingStatus(TrainingStatusEnum.TRAINING_RUNNING.getCode());
		log.debug("Task status checker, got product image size: {}", productImages.size());
		for(ProductImage productImage : productImages) {
			virtualIdentityImageService.queryImages(productImage.getTaskId());
		}


	}

	@Scheduled(cron = "0/30 * * * * ?")
	public void checkWaitingTaskStatus() {
		List<ProductImage> waitingProductImages = productImageQueryService.getByTrainingStatus(TrainingStatusEnum.TRAINING_WAITTING.getCode());
		log.debug("Task status checker, got waiting product image size: {}", waitingProductImages.size());
		for(ProductImage productImage : waitingProductImages) {
			virtualIdentityImageService.queryImages(productImage.getTaskId());
		}
	}

}
 